package com.app.piemoment;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

public class FreshActivity extends Activity {

	private ViewPager viewPager;
	protected ArrayList<View> pageViews;
	private View view;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.guid1);
		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		viewPager = (ViewPager) findViewById(R.id.guidePages);
		pageViews = new ArrayList<View>();
		for (int i = 0; i < 10; i++) {
			view = inflater.inflate(R.layout.guide_item, null, true);
			pageViews.add(view);
		}
		viewPager.setAdapter(new GuidePageAdapter());
		viewPager.setOnPageChangeListener(new GuidePageChangeListener());
	}

	class GuidePageAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			return pageViews.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public int getItemPosition(Object object) {
			return super.getItemPosition(object);
		}

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {
			((ViewPager) arg0).removeView(pageViews.get(arg1));
			Log.e("destroyItem", "destroyItem");
			System.gc();
		}

		@Override
		public Object instantiateItem(View arg0, int arg1) {
			((ViewPager) arg0).addView(pageViews.get(arg1));
			updateUI(arg1, pageViews.get(arg1));
			return pageViews.get(arg1);
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {

		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {

		}

		@Override
		public void finishUpdate(View arg0) {
		}
	}

	class GuidePageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int arg0) {

		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageSelected(int arg0) {

		}
	}

	private Bitmap zapBitmap(int resid) {
		Options opts = new Options();
		opts.inSampleSize = 2;
		Bitmap tempBitmap = BitmapFactory.decodeResource(this.getResources(),
				resid, opts);

		return tempBitmap;
	}

	private void updateUI(int arg1, View view) {
		Bitmap temp = null;
		ImageView iv = (ImageView) view.findViewById(R.id.guideImageview);

		if (0 == arg1) {
			temp = zapBitmap(R.drawable.try_1);
		} else if (1 == arg1) {
			temp = zapBitmap(R.drawable.try_2);
		} else if (2 == arg1) {
			temp = zapBitmap(R.drawable.try_3);
		} else if (3 == arg1) {
			temp = zapBitmap(R.drawable.try_4);
		} else if (4 == arg1) {
			temp = zapBitmap(R.drawable.try_5);
		} else if (5 == arg1) {
			temp = zapBitmap(R.drawable.try_6);
		} else if (6 == arg1) {
			temp = zapBitmap(R.drawable.try_7);
		} else if (7 == arg1) {
			temp = zapBitmap(R.drawable.try_8);
		} else if (8 == arg1) {
			temp = zapBitmap(R.drawable.try_9);
		} else if (9 == arg1) {
			temp = zapBitmap(R.drawable.try_10);
		}

		iv.setImageBitmap(temp);

	}

}
